package com.eduService.Controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.eduService.Service.videoService;
import com.eduService.utils.ConstantProperties;
import com.eduService.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.eduService.utils.InitClient.initVodClient;


@CrossOrigin
@RestController
@RequestMapping("/uploadvideo")
public class videoController {

    @Autowired
    private videoService videoService;

    /*
    * 视频上传
    * */
    @PostMapping("/conduct")
    public R uploadVideo(@RequestBody  MultipartFile file){
        String vid = videoService.uploadVideoAliyun(file);
        return R.ok().data("videoId",vid);
    }

    /*
    * 视频删除
    * */
    @DeleteMapping("/delvideo/{id}")
    public R delVideo(@PathVariable("id") String id) throws ClientException {
        DefaultAcsClient defaultAcsClient = initVodClient(ConstantProperties.KEY_ID, ConstantProperties.KEY_WORD);
        DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();
        deleteVideoRequest.setVideoIds(id);
        DeleteVideoResponse response = new DeleteVideoResponse();
        DeleteVideoResponse acsResponse = defaultAcsClient.getAcsResponse(deleteVideoRequest);
        response = acsResponse;
        return R.ok().msg(response.toString());
    }



    /**
     *
     * 获取视频凭证
     *
     * */
    @GetMapping("/getvideovoucher/{vid}")
    public R getVideoVoucher(@PathVariable("vid") String vid) throws ClientException {
        DefaultAcsClient client = initVodClient(ConstantProperties.KEY_ID, ConstantProperties.KEY_WORD);
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(vid);
        GetVideoPlayAuthResponse acsResponse = client.getAcsResponse(request);
        String playAuth = acsResponse.getPlayAuth();
        return R.ok().data("playAuth",playAuth);
    }

}
